package com.getpebble.android.common.core.util;

import com.getpebble.android.framework.util.ByteUtils;

public class ObjectUtil {
    public static boolean nullCheckEquals(Object first, Object second) {
        if (first == null && second == null) {
            return true;
        }
        if (first == null || second == null) {
            return false;
        }
        return first.equals(second);
    }

    public static String hash(Object object) {
        if (object == null) {
            return "<null>";
        }
        if (object instanceof CharSequence) {
            return obfuscate((CharSequence) object);
        }
        return hashObject(object);
    }

    private static String hashObject(Object object) {
        return ByteUtils.byteArrayToHexString(ByteUtils.int2bytes(object.hashCode()));
    }

    public static String obfuscate(CharSequence text) {
        if (text == null) {
            return null;
        }
        String[] lines = text.toString().split("\n");
        if (lines.length == 0) {
            return null;
        }
        StringBuffer buf = new StringBuffer();
        boolean first = true;
        for (String line : lines) {
            if (first) {
                first = false;
            } else {
                buf.append("\n");
            }
            buf.append(hashObject(line));
        }
        return buf.toString();
    }
}
